﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Modules.DOCUMENT.Components;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using System.Data;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.Controls
{
    public partial class SearchWithMultipleCategories : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                ASPNETVN.PORTAL.Components.Share.Category.BuildDropDownListControlForAll(ref ddlCategory, this.ModuleID);

                ddlPublisher.DataSource = Publisher.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlPublisher.DataBind();

                ddlType.DataSource = ASPNETVN.PORTAL.Modules.DOCUMENT.Components.Type.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlType.DataBind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string where = string.Empty;
            string value = txtSearchValue.Text.Trim();
            if (txtSearchValue.Text.Length > 0)
            {
                if (txtSearchValue.Text.IndexOf("%") < 0)
                {
                    where += string.Format("{0} LIKE N'%{1}%' AND CategoryID LIKE '{2}' AND TypeID LIKE '{3}' AND PublisherID LIKE '{4}'", ddlSearchField.SelectedValue, value, ddlCategory.SelectedValue, ddlType.SelectedValue, ddlPublisher.SelectedValue);
                    DataTable dt = Components.Item.Search(this.ModuleID, where).Tables[0];
                    var query1 = (from r in dt.AsEnumerable()
                                  select new { Code = r.Field<string>("Code"), PublishedDate = r.Field<DateTime>("PublishedDate"), Summary = r.Field<string>("Summary") }).OrderByDescending(x => x.PublishedDate).Distinct();
                    gvItem.DataSource = query1;
                    gvItem.DataBind();
                }
                else
                {
                    this.ShowMessage("Thông báo", "Không chấp nhận các ký tự đặc biệt");
                }
            }
        }
    }
}